package com.platform.server.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.platform.server.service.VipService;

@Controller
public class VipController {
	
	@Autowired
	private VipService vipService;
	
	/**
	 * vip统计
	 */
	@RequestMapping("/selectVipList")
	public Object selectVipList(HttpServletRequest request,String startTime,String endTime,String mobile,String integral_rank){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Map<String, Object>> lists = vipService.selectVipList(startTime, endTime, mobile, integral_rank);
		for(int i = 0;i < lists.size();i++){
			lists.get(i).put("create_time", sdf.format(lists.get(i).get("create_time")));
		}
		request.setAttribute("lists", lists);
		request.setAttribute("nums", lists.size());
		return "ui/vip/vip_list.jsp";
	}
	@RequestMapping({"/selectAgreement"})
	  public Object selectAgreement(HttpServletRequest request, String startTime, String endTime, String mobile, String integral_rank)
	  {
	    request.setAttribute("lists", this.vipService.selectAgreement());
	    return "ui/vip/vip_agreement.jsp";
	  }
	  
	  @RequestMapping({"/vipAgreement"})
	  public void vipAgreement(HttpServletRequest request, String entvip,String id, String vip1, String vip2, HttpServletResponse response)
	    throws Exception
	  {
	    Map<String, Object> selectAgreement = this.vipService.selectAgreement();
	    if (selectAgreement == null)
	    {
	      Map<String, Object> map = new HashMap();
	      map.put("vip1", vip1);
	      map.put("vip2", vip2);
	      map.put("entvip", entvip);
	      int flag = this.vipService.intsertVipAgreement(map);
	      JSONObject jsonObject = new JSONObject();
	      jsonObject.put("flag", flag);
	      response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	    }
	    else
	    {
	      Map<String, Object> map = new HashMap();
	      map.put("vip1", vip1);
	      map.put("id", id);
	      map.put("vip2", vip2);
	      map.put("entvip", entvip);
	      int flag = this.vipService.updateVipAgreement(map);
	      JSONObject jsonObject = new JSONObject();
	      jsonObject.put("flag", flag);
	      response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	    }
	  }
	  
	  @RequestMapping({"/selectEquity"})
	  public Object selectEquity(HttpServletRequest request, String startTime, String endTime, String mobile, String integral_rank)
	  {
	    request.setAttribute("lists", this.vipService.selectEquity());
	    return "ui/vip/vip_equity.jsp";
	  }
	  
	  @RequestMapping({"/vipEquity"})
	  public void vipEquity(HttpServletRequest request, String entvip,String id, String vip1, String vip2, HttpServletResponse response)
	    throws Exception
	  {
	    Map<String, Object> selectAgreement = this.vipService.selectEquity();
	    if (selectAgreement == null)
	    {
	      Map<String, Object> map = new HashMap();
	      map.put("vip1", vip1);
	      map.put("vip2", vip2);
	      map.put("entvip", entvip);
	      int flag = this.vipService.intsertVipEquity(map);
	      JSONObject jsonObject = new JSONObject();
	      jsonObject.put("flag", flag);
	      response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	    }
	    else
	    {
	      Map<String, Object> map = new HashMap();
	      map.put("vip1", vip1);
	      map.put("id", id);
	      map.put("vip2", vip2);
	      map.put("entvip", entvip);
	      int flag = this.vipService.updateVipEquity(map);
	      JSONObject jsonObject = new JSONObject();
	      jsonObject.put("flag", flag);
	      response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	    }
	  }
}
